﻿#include "GeneralMeowMeow.h"
#include "../../Settings.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../MapObject.h"
#include "../SpellObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char GeneralMeowMeow::getAttackRange() const
    {
        return 12;
    }

    GeneralMeowMeow::GeneralMeowMeow(MonsterInfo *info) : MonsterObject(info)
    {
        ShieldUpDuration = Settings::Second * 30;
    }

    bool GeneralMeowMeow::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    void GeneralMeowMeow::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 2);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        /* Energy Shield Logic:
           When mob gets to certain health percentages (i.e. 80% / 60% / 40% / 20%) active Energy Shield.
           Whilst Energy Shield is active the following happen:
                - This monsters damage taken is reduced (50% reduction?)
                - Every 'x' amount of seconds all players in range are attacked with a thunderbolt
         */

        auto hpPercent = (HP * 100) / Stats[Stat::HP];

        bool stage1Bubble = hpPercent >= 70 && hpPercent <= 80; //HP < Stats[Stat.HP] / 10 * 8 && this.HP > Stats[Stat.HP] / 10 * 7;
        bool stage2Bubble = hpPercent >= 40 && hpPercent <= 50; //HP < Stats[Stat.HP] / 10 * 5 && this.HP > Stats[Stat.HP] / 10 * 4;
        bool stage3Bubble = hpPercent <= 20; //this.HP < Stats[Stat.HP] / 10 * 2 && this.HP > 1;

        if (stage1Bubble == true || stage2Bubble == true || stage3Bubble == true)
        {
            if (getTarget() != nullptr)
            {
                auto stats = new Stats();
                stats.[Stat::MaxAC] = 100;
                stats.[Stat::MinAC] = 100;

                AddBuff(BuffType::GeneralMeowMeowShield, this, ShieldUpDuration, stats, visible: true);

                if (getEnvir()->getTime() > ThunderAttackTime)
                {
                    MassThunderAttack();
                }

                OperateTime = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete stats' statement was not added since stats was passed to a method or constructor. Handle memory management manually.
            }
        }

        if (!ranged)
        {
            if (getEnvir()->Random->Next(9) != 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 0;
                Broadcast(tempVar);
                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility, false});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 1;
                Broadcast(tempVar2);
                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]) * 3;
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::AC, true});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->TargetID = getTarget()->ObjectID;
            tempVar3->Type = 0;
            Broadcast(tempVar3);
            int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GeneralMeowMeow::ProcessAI()
    {
        if (Dead)
        {
            return;
        }

        // After first 60 seconds: spawn mobs, then every 60 seconds after, spawn more mobs.
        if (getTarget() != nullptr && getEnvir()->getTime() > SlaveSpawnTime)
        {
            SpawnSlaves();
            SlaveSpawnTime = getEnvir()->getTime() + (Settings::Second * 60);
        }

        MonsterObject::ProcessAI();
    }

    void GeneralMeowMeow::MassThunderAttack()
    {
        // Whilst Energy Shield is up, attack all players every few seconds.
        if (getEnvir()->getTime() > ThunderAttackTime)
        {
            std::vector<MapObject*> targets = FindAllTargets(getAttackRange(), getTarget()->getCurrentLocation());
            if (targets.empty())
            {
                return;
            }

            for (int i = 0; i < targets.size(); i++)
            {
                if (targets[i]->IsAttackTarget(this))
                {
                    auto spellObj = new SpellObject();
                    spellObj->Spell = Spell::GeneralMeowMeowThunder;
                    spellObj->Value = getEnvir()->Random->Next(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
                    spellObj->ExpireTime = getEnvir()->getTime() + 1000;
                    spellObj->TickSpeed = 500;
                    spellObj->Caster = nullptr;
                    spellObj->setCurrentLocation(targets[i]->getCurrentLocation());
                    spellObj->setCurrentMap(getCurrentMap());
                    spellObj->setDirection(MirDirection::Up);

                    DelayedAction *action = new DelayedAction(DelayedType::Spawn, getEnvir()->getTime() + 2000, {spellObj});
                    getCurrentMap()->ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete spellObj' statement was not added since spellObj was passed to a method or constructor. Handle memory management manually.
                }
            }
        }

        ThunderAttackTime = getEnvir()->getTime() + std::max(getEnvir()->Random->Next(2000), getEnvir()->Random->Next(4000));
    }

    void GeneralMeowMeow::Spawned()
    {
        // Begin countdown timer
        SlaveSpawnTime = getEnvir()->getTime() + (Settings::Second * 60);

        MonsterObject::Spawned();
    }

    void GeneralMeowMeow::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        bool slamDamage = std::any_cast<bool>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        target->Attacked(this, damage, defence);
    }

    void GeneralMeowMeow::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(2, getTarget()->getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            targets[i]->Attacked(this, damage, defence);
        }
    }

    void GeneralMeowMeow::SpawnSlaves()
    {
        int count = std::min(3, 6 - SlaveList.size());

        for (int i = 0; i < count; i++)
        {
            MonsterObject *mob = nullptr;
            switch (getEnvir()->Random->Next(4))
            {
                case 0:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::GeneralMeowMeowMob1));
                    break;
                case 1:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::GeneralMeowMeowMob2));
                    break;
                case 2:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::GeneralMeowMeowMob3));
                    break;
                case 3:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::GeneralMeowMeowMob4));
                    break;
            }

            if (mob == nullptr)
            {
                continue;
            }

            if (!mob->Spawn(getCurrentMap(), getFront()))
            {
                mob->Spawn(getCurrentMap(), getCurrentLocation());
            }

            mob->setTarget(getTarget());
            mob->ActionTime = getEnvir()->getTime() + 2000;
            SlaveList.push_back(mob);
        }
    }

    void GeneralMeowMeow::ProcessTarget()
    {
        if (getCurrentMap()->Players.empty())
        {
            return;
        }

        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
